<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta content="width=device-width,initial-scale=1.0,maximum-scale=1.0,user-scalable=0" name="viewport"/>
    <meta content="yes" name="apple-mobile-web-app-capable"/>
    <meta content="black" name="apple-mobile-web-app-status-bar-style"/>
    <meta content="telephone=no" name="format-detection"/>
    <meta charset="UTF-8">
    <meta content="width=device-width,initial-scale=1.0,maximum-scale=1.0,user-scalable=0" name="viewport"/>
    <meta content="yes" name="apple-mobile-web-app-capable"/>
    <meta content="black" name="apple-mobile-web-app-status-bar-style"/>
    <meta content="telephone=no" name="format-detection"/>
    <!--引入初始化样式-->
    <link rel="stylesheet" href="../../b2c/lib/css/style-min.css">
    <!--引入YUDI样式-->
    <link rel="stylesheet" href="../../b2c/lib/css/ydui.css">
    <!--引入YUDI自适应解决方案类库-->
    <script src="../../b2c/lib/js/flexible/flexible.js"></script>
    <!-- 引入jQuery 2.0+ -->
    <script src="../../b2c/lib/js/jquery/jquery-2.1.4.min.js"></script>
    <!-- 引入common -->
    <script src="../lib/js/common/common.js"></script>
    <!-- 引入vue -->
    <script src="../../b2c/lib/js/vue/vue.min.js"></script>
    <!--引入对应的css-->
    <link rel="stylesheet" href="../css/user/user_login.css">
</head>
<body class="register-body" style="background-color: #eee">
<div class="register-con" id="app" v-show="appShow" style="display: none">
    <div style="width: 100%;height: 15px;"></div>
    <div class="code-box">
        <span class="num">手机号</span>
        <input class="code" type="number" id="phone" placeholder="请输入手机号码"  v-model="mobile">
    </div>
    <div class="code-box">
        <span class="num">验证码</span>
        <input class="code code-2" type="text" id="code-id" placeholder="验证码" v-model="code">
        <button class="send-code" id="send-code-id" @click="sendCode">发送验证码</button>
    </div>
    <div class="explain">
        <img src="../images/user/tishi.png"><span>登录自动注册</span>
    </div>
    <div class="register-btn">
        <a href="javascript:;"  @click="logging()">绑定并登录</a>
    </div>
</div>
<script src="../lib/js/ydui.js"></script>
<script type="text/javascript">
    com_obj.access_token = true;
    var app = new Vue({
        el:"#app",
        data: {
            appShow:false,
            mobile:'',
            code:'',
            openid:'',
            isSendCode:true
        },
        filters:{
            formatImg:function (value) {
                var _value=  '/images/wineshop/list_default.png';
                if(value.length>0){
                    _value = value[0];
                }
                return _value;
            }
        },
        created: function () {
            // `this` 指向 vm 实例
        },
        mounted:function () {
            this.$nextTick(function () {

            });
        },
        methods: {
            sendCode:function () {
                if(this.isSendCode){
                    sendCode();
                }
            },
            logging:function () {
                if (this.mobile==""){
                    YDUI.dialog.toast("请先输入手机号码",'none',function(){});
                }
                else if (this.mobile.toString().length !=11) {
                    YDUI.dialog.toast("手机号码格式不正确",'none',function(){});
                }
                else if (this.code=='') {
                    YDUI.dialog.toast("请输入验证码",'none',function(){});
                }
                else if (this.code.toString().length<4) {
                    YDUI.dialog.toast("验证码长度不低于4",'none',function(){});
                }else {
                    getInfo();
                }
            }
        }
    });
    $(function(){

            init();


    });
    function PageInit(){

    }
    function appInit(){
        PageInit();
    }
    function init() {
        $.ajaxHandler({
            model: function () {
                var obj = {};
                obj.code = $.getPara("code");
                obj.type =1;// 0只获取openId，1同时获取用户信息
//
                console.log(obj)
                return obj;
            },
            visibleTime: 0,
            dataType: "json",
            approve: true,
            apiUrl:$.appApi(),
            api: 'yg_baijiaapp/unDefinedUser/authorization',
            success: function (result) {

                if (result.code == 10000) {
                    if(result.data.userId!=0){
                        saveInfo(result.data);
                    }else{

                        window.localStorage.setItem("xmOpenid",result.data.undefinedId);
                        if($.getStorage("openUrl").indexOf('inviterUserId')>-1){
                            var obj={};
                            obj.openId=result.data.undefinedId;
                            $.setStorage("xm_uData",obj);
                            window.location.href=$.getStorage("openUrl");
                        }else if($.getStorage("openUrl").indexOf('storeId')>-1){
                            var obj={};
                            obj.openId=result.data.undefinedId;
                            saveInfo(result.data);

                        }else if($.getStorage("openUrl").indexOf('recommendFrom')>-1){
                            var obj={};
                            obj.openId=result.data.undefinedId;
                            $.setStorage("xm_uData",obj);
                            window.location.href=$.getStorage("openUrl")
                        }
                        else{
                            app.openid =result.data.undefinedId;
                            app.appShow = true;
                            document.title='立即登录';
                        }

                    }
                }else {
                    YDUI.dialog.toast(result.message+result.code,'error',1000);
                }
            }
        });
    }
    function getInfo() {
        $.ajaxHandler({
            model: function () {
                var obj = {};
                obj.mobile = app.mobile;
                obj.validCode = app.code;
                obj.undefinedId = app.openid;
                obj.undefinedType =2; //第三方登录类型1APP 2H5
                console.log(obj)
                return obj;
            },
            visibleTime: 0,
            dataType: "json",
            approve: true,
//          apiUrl:$.appApi(),
            api: 'yg_baijiaapp/unDefinedUser/getUndefinedUserAdd',
            success: function (result) {
                if (result.code == 10000){
                    $.setStorage('recommendFrom',0)
                    saveInfo(result.data);
                } else {
                    YDUI.dialog.toast(result.message,'error',1000);
                }
            }
        });
    }

    //发送短信验证码
    function sendCode() {
        if ($("#phone").val() == ""){
            YDUI.dialog.toast("请先输入手机号码",'none',function(){});
        } else if ($("#phone").val().length != 11){
            YDUI.dialog.toast("手机号码格式不正确",'none',function(){});
        } else {
            $("#send-code-id").attr("disabled","disabled");
            $.ajaxHandler({
                        model: function () {
                            var obj = {};
                            obj.mobile = $("#phone").val();
                            obj.type = 1;
                            return obj;
                        },
                        visibleTime: 100,
                        dataType: "json",
                        apiUrl:$.appApi(),
                        api: 'yg_baijiaapp/CheckCode/sendVerificationCode',
                        success: function (result) {
                            //console.log(result);
                            if (result.code == 10000) {
                                app.isSendCode = false;
                                YDUI.dialog.toast('已发送', 'success', 1000);
                                SendTime("#send-code-id");
                            }else {
                                YDUI.dialog.toast(result.message,'none',function(){});
                            }

                        }
                    }
            );
        }
    }

    var wait = 60;//短信时间
    function SendTime(parameter) {
        var Send = $("" + parameter + "");
        if (wait == 0) {
            app.isSendCode = true;
            Send.html("点击获取验证码");
            wait = 60;
            $("#send-code-id").removeAttr("disabled");
        } else {
            Send.html("重新发送(" + wait + ")s");
            wait--;
            setTimeout(function () {
                SendTime(parameter);
            }, 1000);
        }
    }
</script>
</body>
</html>
